﻿using Hotel.Application.Suppliers;
using Hotel.Application.Suppliers.Dto;
using Hotel.Domain;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace Hotel.Web.Controllers
{
    /// <summary>
    /// 供应商
    /// </summary>
    public class SupplierController : Controller
    {
        private readonly ISupplierService supplierService;

        public SupplierController(ISupplierService supplierService)
        {
            this.supplierService = supplierService;
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public async Task<IActionResult> Page([FromQuery]SupplierQueryDto queryDto,int PageSize = 10,int PageIndex = 1)
        {
            var expressionable = Expressionable.Create<Supplier>();
            expressionable.AndIF(!string.IsNullOrEmpty(queryDto.SupplierName),m => m.SupplierName.Contains(queryDto.SupplierName));
            expressionable.AndIF(!string.IsNullOrEmpty(queryDto.SupplierCode),m => m.SupplierCode.Contains(queryDto.SupplierCode));
            expressionable.AndIF(queryDto.SupplierType != null,m => m.SupplierType == queryDto.SupplierType);
            expressionable.AndIF(!string.IsNullOrEmpty(queryDto.Phone),m => m.Phone.Contains(queryDto.Phone));

            var pa = await supplierService.ToPageListAsync(m => m.CreateTime, expressionable, PageSize, PageIndex);
            return Json(pa);
        }

        [HttpGet]
        public IActionResult Create()
        {
            return View(new SupplierCreateUpdateDto {
                SupplierCode = $"GW{DateTime.Now.ToString("yyyyMMddHHmmss")}"
            });
        }

        [HttpPost]
        public async Task<IActionResult> Create(SupplierCreateUpdateDto dto)
        {
            var result = await supplierService.CreateAsync(dto);
            return Ok(result);
        }

        [HttpGet]
        public async Task<IActionResult> MockData()
        {
            await supplierService.MockDataAsync();
            return Ok();
        }
    }
}
